package com.ruoyi.zyx.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zyx.mapper.ZyxMemberMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyx.domain.ZyxMember;
import com.ruoyi.zyx.service.IZyxMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成员基础信Controller
 *
 * @author lhs
 * @date 2021-05-25
 */
@RestController
@RequestMapping("/zyx/member")
public class ZyxMemberController extends BaseController
{
    @Autowired
    private IZyxMemberService zyxMemberService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询成员基础信列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyxMember zyxMember)
    {
        startPage();
        List<ZyxMember> list = zyxMemberService.selectZyxMemberList(zyxMember);
        return getDataTable(list);
    }



    /**
     * 导出成员基础信列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:member:export')")
    @Log(title = "成员基础信", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZyxMember zyxMember)
    {
        List<ZyxMember> list = zyxMemberService.selectZyxMemberList(zyxMember);
        ExcelUtil<ZyxMember> util = new ExcelUtil<ZyxMember>(ZyxMember.class);
        return util.exportExcel(list, "成员基础信数据");
    }

    /**
     * 获取成员基础信详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zyxMemberService.selectZyxMemberById(id));
    }


    /**
     * 获取成员基础信详细信息
     */
    @GetMapping(value = "/getMemberInfo")
    public AjaxResult getMemberInfo()
    {
//        System.out.println(SecurityUtils.getLoginUser().getUser());
        ZyxMember zyxMember=zyxMemberService.selectZyxMemberByUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return AjaxResult.success(zyxMember);
    }

    /**
     * 新增成员基础信
     */
    @PreAuthorize("@ss.hasPermi('zyx:member:add')")
    @Log(title = "成员基础信", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyxMember zyxMember)
    {
        if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkUserNameUnique(zyxMember.getMobile())))
        {
            return AjaxResult.error("新增成员'" + zyxMember.getName() + "'失败，手机号账号已存在");
        }

        zyxMember.setDepartid(SecurityUtils.getLoginUser().getUser().getDeptId());
        return toAjax(zyxMemberService.insertZyxMember(zyxMember));
    }

    /**
     * 修改成员基础信
     */
    @PreAuthorize("@ss.hasPermi('zyx:member:edit')")
    @Log(title = "成员基础信", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyxMember zyxMember)
    {
        SysUser sysUser=new SysUser();
        sysUser.setUserName(zyxMember.getMobile());
        sysUser.setUserId(zyxMember.getUserid());
        if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkUserNameEditUnique(sysUser)))
        {
            return AjaxResult.error("修改成员'" + zyxMember.getName() + "'失败，手机号账号已存在");
        }
        return toAjax(zyxMemberService.updateZyxMember(zyxMember));
    }

    /**
     * 删除成员基础信
     */
    @PreAuthorize("@ss.hasPermi('zyx:member:remove')")
    @Log(title = "成员基础信", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zyxMemberService.deleteZyxMemberByIds(ids));
    }
}
